﻿using System.Collections.Generic;
using UnityEngine;

public class BulletPool:MonoBehaviour
{
    public int InitSize;
    public GameObject Bullet;

    private Queue<Bullet> bullets;

    public void Start()
    {
        bullets = new Queue<Bullet>(InitSize);
        for (int i = 0; i < InitSize; i++)
        {
            GameObject go =  Instantiate(Bullet,transform);
            Bullet bullet = go.GetComponent<Bullet>();
            bullet.SetPool(this);
            bullet.Restore();
            bullets.Enqueue(bullet);
        }
    }

    public void Fire(Vector3 position,bool direction)
    {
        if (bullets.Count == 0)
            return;
        Bullet bullet = bullets.Dequeue();
        bullet.gameObject.transform.position = position;
        bullet.Fire(direction);
    }

    public void Restore(Bullet bullet)
    {
        bullet.Restore();
        bullets.Enqueue(bullet);
    }
}
